import { reactive } from "vue";
import { customForm } from "@/components/ProForm/interface/index";
import { User } from "@/api/interface";
import { LOGIN_URL } from "@/config";

const userStore = useUserStore();

let changePwdForm = reactive<customForm<User.ReqChangePassword>>({
  form: {
    username: "",
    lodPassword: "",
    newPassword: ""
  },
  title: "",
  visible: false,
  formCols: 1,
  labelWidth: [70, 90],
  formAttrs: {
    labelWidth: ""
  },
  options: [
    {
      type: "input",
      label: "username",
      hide: false,
      prop: "username",
      defaultValue: userStore.username,
      rules: [{ required: true, message: "username", trigger: "blur" }],
      attrs: {
        disabled: true
      }
    },
    {
      type: "input",
      label: "oldPassword",
      hide: false,
      defaultValue: "",
      prop: "oldPassword",
      rules: [{ required: true, message: "oldPassword", trigger: "blur" }]
    },
    {
      type: "input",
      label: "newPassword",
      hide: false,
      defaultValue: "",
      prop: "newPassword",
      rules: [{ required: true, message: "newPassword", trigger: "blur" }]
    }
  ]
});

const savePwdForm = async (form: User.ReqChangePassword) => {
  await userUpPasswordByUser(form);
  changePwdForm.visible = false;
  // 1.执行退出登录接口
  await logoutApi();

  // 2.清除 Token
  userStore.setToken("");

  // 3.重定向到登陆页
  routerReplace(LOGIN_URL);
  elMessage(transitionLanguages("basic", "changePasswordSuccess"));
};
export { changePwdForm, savePwdForm };
